const express = require("express");
const router = express.Router();
const { exec } = require("child_process");
const ResponseHelper = require("../ResponseHelper");
const os = require('os')
router.get("/", (req, res, next) => {
  //获取操作系统
  const system =  os.platform();
  let commandStr = 'shutdown -s -t 00';
  if(system === 'linux'){
    commandStr = 'shutdown -h now'
  }
  console.log('system',system)
  let command = exec(commandStr, function (err, stdout, stderr) {
    if (err || stderr) {
      console.log("shutdown failed" + err + stderr);
      ResponseHelper.sendData(`shutdown failed,err:${err},stderr:${stderr}`);
    }
  });
  command.stdin.end();
  command.on("close", function (code) {
    ResponseHelper.sendData(`shutdown${code}`, res);
    console.log("shutdown", code);
  });
});

module.exports = router;
